package com.yoyo.ad.confusion;

import android.content.Context;
import android.util.Log;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.TTAdNative;
import com.yoyo.ad.api.YoYoConfig;
import com.yoyo.ad.baidu.BaiduAdFactory;
import com.yoyo.ad.gdt.GDTAdFactory;
import com.yoyo.ad.main.IAdBannerListener;
import com.yoyo.ad.main.IAdFactory;
import com.yoyo.ad.main.IAdFactoryListener;
import com.yoyo.ad.main.IAdFactory_;
import com.yoyo.ad.main.IAdInteractionListener;
import com.yoyo.ad.main.IAdSource;
import com.yoyo.ad.main.IAdView;
import com.yoyo.ad.service.RequestService;
import com.yoyo.ad.tencent.TencentAdFactory;
import com.yoyo.ad.toutiao.TouTiaoAdFactory;
import com.yoyo.ad.utils.AnalysisUtils;
import com.yoyo.ad.yoyo.YYAdFactory;
import com.yoyo.ad.yoyoplat.YPFactory;

import static com.yoyo.ad.confusion.AdManage.BAIDU;
import static com.yoyo.ad.confusion.AdManage.GDT;
import static com.yoyo.ad.confusion.AdManage.TENCENT;
import static com.yoyo.ad.confusion.AdManage.TOU_TIAO;
import static com.yoyo.ad.confusion.AdManage.YOYO;
import static com.yoyo.ad.confusion.AdManage.YOYO_PLAT;
import static com.yoyo.ad.confusion.AdManage.getAdType;


public class AdFactory implements IAdFactory {

    private EmptyAdFactory mEmptyAdFactory;
    private BaiduAdFactory mBaiduAdFactory;
    private TencentAdFactory mTencentAdFactory;
    private TouTiaoAdFactory mTouTiaoAdFactory;
    private GDTAdFactory mGDTAdFactory;
    private YYAdFactory mYYAdFactory;
    private YPFactory mYPFactory;

    private IAdView mAdView;
    private IAdBannerListener mAdBannerListener;
    private IAdInteractionListener mAdInteractionListener;
    private Context mContext;
    private TTAdNative mTTAdBean;

    public AdFactory(Context context, IAdView adView, TTAdNative ttAdBean) {
        mAdView = adView;
        mContext = context;
        mTTAdBean = ttAdBean;
    }

    public AdFactory(Context context, IAdBannerListener adView, TTAdNative ttAdBean) {
        mAdBannerListener = adView;
        mContext = context;
        mTTAdBean = ttAdBean;
    }

    public AdFactory(Context context, IAdInteractionListener adView, TTAdNative ttAdBean) {
        mAdInteractionListener = adView;
        mContext = context;
        mTTAdBean = ttAdBean;
    }

    @Override
    public void getYoyoAd(final int position, final int requestCode, final int size, final int type) {
        getAdFactory(position, new IAdFactoryListener() {
            @Override
            public void onAdFactory(IAdFactory_ adFactoryListener, String adPlace) {
                adFactoryListener.getYoyoAd(position, requestCode, size, adPlace, type);
            }
        });
    }

    @Override
    public void getExpressAd(final int position, final int requestCode, final int size, final int width) {
        getAdFactory(position, new IAdFactoryListener() {
            @Override
            public void onAdFactory(IAdFactory_ adFactoryListener, String adPlace) {
                adFactoryListener.getExpressAd(position, requestCode, adPlace, size, width);
            }
        });
    }

    @Override
    public void getBanner(final int position, final int requestCode, final ViewGroup adsParent, final int width, final int height) {
//        if (!AdLocalSourceRecord.isBannerChange(mContext)) {
//            return;
//        }
        getAdFactory(position, new IAdFactoryListener() {
            @Override
            public void onAdFactory(IAdFactory_ adFactoryListener, String adPlace) {
//                AdLocalSourceRecord.setBannerChange(mContext);
                adFactoryListener.getBanner(position, requestCode, adPlace, adsParent, width, height);
            }
        });
    }

    @Override
    public void getInteraction(final int position, final int requestCode) {
        getAdFactory(position, new IAdFactoryListener() {
            @Override
            public void onAdFactory(IAdFactory_ adFactoryListener, String adPlace) {
                adFactoryListener.getInteraction(position, requestCode, adPlace);
            }
        });
    }

    @Override
    public void destroy() {
        RequestService.newInstance(mContext, RequestService.FLAG_STATISTICS_REPORT);
        if (mBaiduAdFactory != null)
            mBaiduAdFactory.onDestroy();
        if (mTencentAdFactory != null)
            mTencentAdFactory.onDestroy();
        if (mTouTiaoAdFactory != null)
            mTouTiaoAdFactory.onDestroy();
        if (mGDTAdFactory != null)
            mGDTAdFactory.onDestroy();
        if (mYYAdFactory != null)
            mYYAdFactory.onDestroy();
        if (mYPFactory != null)
            mYPFactory.onDestroy();
    }

    private void getAdFactory(final int position, final IAdFactoryListener adFactoryListener) {
        AnalysisUtils.addStatistics(-1, AnalysisUtils.TYPE_START);
        getAdType(mContext, position, new IAdSource() {
            @Override
            public void adSource(int adSource, long adId, String adPlaceId, boolean isIpReport) {
                Log.e(YoYoConfig.TAG, "adSource:" + adSource);
                IAdFactory_ iAdFactory = getIAdFactory_(adSource);
                AnalysisUtils.addStatistics(adId, AnalysisUtils.TYPE_REQUEST);
                iAdFactory.setConfig(adId, position, isIpReport);
                adFactoryListener.onAdFactory(iAdFactory, adPlaceId);
            }
        });
    }

    private IAdFactory_ getIAdFactory_(int adSource) {
        IAdFactory_ iAdFactory;
        switch (adSource) {
            case BAIDU:
                if (mBaiduAdFactory == null) {
                    mBaiduAdFactory = new BaiduAdFactory(mContext);
                }
                mBaiduAdFactory.setAdView(mAdView);
                mBaiduAdFactory.setAdBannerListener(mAdBannerListener);
                mBaiduAdFactory.setAdInteractionListener(mAdInteractionListener);
                iAdFactory = mBaiduAdFactory;
                break;
            case TENCENT:
                if (mTencentAdFactory == null) {
                    mTencentAdFactory = new TencentAdFactory(mContext);
                }
                mTencentAdFactory.setAdView(mAdView);
                mTencentAdFactory.setAdBannerListener(mAdBannerListener);
                mTencentAdFactory.setAdInteractionListener(mAdInteractionListener);
                iAdFactory = mTencentAdFactory;
                break;
            case GDT:
                if (mGDTAdFactory == null) {
                    mGDTAdFactory = new GDTAdFactory(mContext);
                }
                mGDTAdFactory.setAdView(mAdView);
                mGDTAdFactory.setAdBannerListener(mAdBannerListener);
                mGDTAdFactory.setAdInteractionListener(mAdInteractionListener);
                iAdFactory = mGDTAdFactory;
                break;
            case YOYO:
                if (mYYAdFactory == null) {
                    mYYAdFactory = new YYAdFactory(mContext);
                }
                mYYAdFactory.setAdView(mAdView);
                mYYAdFactory.setAdBannerListener(mAdBannerListener);
                mYYAdFactory.setAdInteractionListener(mAdInteractionListener);
                iAdFactory = mYYAdFactory;
                break;
            case YOYO_PLAT:
                if (mYPFactory == null) {
                    mYPFactory = new YPFactory(mContext);
                }
                mYPFactory.setAdView(mAdView);
                mYPFactory.setAdBannerListener(mAdBannerListener);
                mYPFactory.setAdInteractionListener(mAdInteractionListener);
                iAdFactory = mYPFactory;
                break;
            case TOU_TIAO:
                //必须放在最后一个case,当context没实现TTAdView接口时可返回空工厂类
                if (mTTAdBean != null) {
                    if (mTouTiaoAdFactory == null) {
                        mTouTiaoAdFactory = new TouTiaoAdFactory(mContext, mTTAdBean);
                    }
                    mTouTiaoAdFactory.setAdView(mAdView);
                    mTouTiaoAdFactory.setAdBannerListener(mAdBannerListener);
                    mTouTiaoAdFactory.setAdInteractionListener(mAdInteractionListener);
                    iAdFactory = mTouTiaoAdFactory;
                    break;
                }
            default:
                if (mEmptyAdFactory == null) {
                    mEmptyAdFactory = new EmptyAdFactory();
                }
                mEmptyAdFactory.setAdView(mAdView);
                mEmptyAdFactory.setAdBannerListener(mAdBannerListener);
                mEmptyAdFactory.setAdInteractionListener(mAdInteractionListener);
                AnalysisUtils.addStatistics(-2, AnalysisUtils.TYPE_INTERCEPT);
                iAdFactory = mEmptyAdFactory;
                break;
        }
        return iAdFactory;
    }
}
